package com.zhm.demo.netty.handler;


import com.zhm.demo.netty.protobuf.DeviceBean;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @title
 * @description
 * @author hufei
 * @updateTime 2024/1/6 10:38
 */
@Slf4j
public class ProtoServerHandler extends SimpleChannelInboundHandler<DeviceBean.WrapperMessage> {


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DeviceBean.WrapperMessage msg) {
//        System.out.println(msg.getType());
//        System.out.println(msg.getUser().toString());
        //因为对象pipeline进来的 所以Netty直接使用SpringUtils 去容器里面取即可
        log.info("{}",msg);
        switch (msg.getType()) {
            case IotNettyUser -> {

            }
            case Ping -> {
                DeviceBean.Ping ping = msg.getPing();
            }
            case Pong -> {
                DeviceBean.Pong pong = msg.getPong();
            }
            default -> {
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 异常处理逻辑
        // 这里您可以记录日志、关闭连接或者其他处理方式
        cause.printStackTrace(); // 打印异常堆栈信息
        ctx.close(); // 关闭连接
    }
}
